home *** CD-ROM | disk | FTP | other *** search
/ Clickx 23 / Clickx 23.iso / DATA / moveabletype / MT-3.2-en_US / mt-db2sql.cgi < prev    next >
Encoding:
Text File  |  2005-07-29  |  5.9 KB  |  178 lines

  1. #!/usr/bin/perl -w
  2.  
  3. # Copyright 2001-2005 Six Apart. This code cannot be redistributed without
  4. # permission from www.sixapart.com.  For more information, consult your
  5. # Movable Type license.
  6. #
  7. # $Id: mt-db2sql.cgi 15432 2005-07-29 20:41:11Z bchoate $
  8.  
  9. use strict;
  10. sub BEGIN {
  11.     my $dir;
  12.     require File::Spec;
  13.     if (!($dir = $ENV{MT_HOME})) {
  14.         if ($0 =~ m!(.*[/\\])!) {
  15.             $dir = $1;
  16.         } else {
  17.             $dir = './';
  18.         }
  19.         $ENV{MT_HOME} = $dir;
  20.     }
  21.     unshift @INC, File::Spec->catdir($dir, 'lib');
  22.     unshift @INC, File::Spec->catdir($dir, 'extlib');
  23. }
  24.  
  25. local $| = 1;
  26. print "Content-Type: text/html\n\n";
  27. print "<pre>\n\n";
  28.  
  29. my @CLASSES = qw( MT::Author MT::Blog MT::Category MT::Comment MT::Entry
  30.                   MT::IPBanList MT::Log MT::Notification MT::Permission
  31.                   MT::Placement MT::Template MT::TemplateMap MT::Trackback
  32.                   MT::TBPing MT::Session MT::PluginData MT::Config );
  33.  
  34. use File::Spec;
  35.  
  36. eval {
  37.     local $SIG{__WARN__} = sub { print "**** WARNING: $_[0]\n" };
  38.  
  39.     require MT;
  40.     my $mt = MT->new() or die MT->errstr;
  41.  
  42.     my $cfg = $mt->{cfg};
  43.     require MT::Object;
  44.     my($type) = $cfg->ObjectDriver =~ /^DBI::(.*)$/;
  45.     MT::Object->set_driver('DBI::' . $type)
  46.         or die MT::ObjectDriver->errstr;
  47.     my $dbh = MT::Object->driver->{dbh};
  48.  
  49.     use MT::Upgrade;
  50.     my @stmts;
  51.     foreach (@CLASSES) {
  52.         push @stmts, MT::Upgrade->check_class($_);
  53.     }
  54.     print "Loading database schema...\n\n";
  55.     for my $stmt (@stmts) {
  56.         next if ref($stmt); # Skip triggers??? What if user is upgrading
  57.                             # into a populated, old schema? Should we prevent
  58.                             # that scenario?
  59.         $stmt =~ s!^\s*!!;
  60.         $stmt =~ s!\s*$!!;
  61.         next unless $stmt =~ /\S/;
  62.         $dbh->do($stmt) or die $dbh->errstr;
  63.     }
  64.  
  65.     ## %ids will hold the highest IDs of each class.
  66.     my %ids;
  67.  
  68.     print "Loading data...\n";
  69.     for my $class (@CLASSES, 'MT::FileInfo' ) {
  70.         print $class, "\n";
  71.         MT::Object->set_driver('DBM');
  72.         eval "use $class";
  73.         my $iter = $class->load_iter;
  74.  
  75.         my %names;
  76.         my %cat_parent;
  77.  
  78.         MT::Object->set_driver('DBI::' . $type);
  79.         while (my $obj = $iter->()) {
  80.             print "    ", $obj->id, "\n";
  81.             $ids{$class} = $obj->id
  82.                 if !$ids{$class} || $obj->id > $ids{$class};
  83.             ## Look for duplicate template, category, and author names,
  84.             ## because we have uniqueness constraints in the DB.
  85.             if ($class eq 'MT::Template') {
  86.                 my $key = lc($obj->name) . $obj->blog_id;
  87.                 if ($names{$class}{$key}++) {
  88.                     print "        Found duplicate template name '" .
  89.                           $obj->name;
  90.                     $obj->name($obj->name . ' ' . $names{$class}{$key});
  91.                     print "'; renaming to '" . $obj->name . "'\n";
  92.                 }
  93.                 ## Touch the text column to make sure we read in
  94.                 ## any linked templates.
  95.                 my $text = $obj->text;
  96.             } elsif ($class eq 'MT::Author') {
  97.                 my $key = lc($obj->name);
  98.                 if ($names{$class . $obj->type}{$key}++) {
  99.                     print "        Found duplicate author name '" .
  100.                           $obj->name;
  101.                     $obj->name($obj->name . ' ' . $names{$class}{$key});
  102.                     print "'; renaming to '" . $obj->name . "'\n";
  103.                 }
  104.                 $obj->email('') unless defined $obj->email;
  105.                 $obj->set_password('') unless defined $obj->password;
  106.             } elsif ($class eq 'MT::Comment') {
  107.                 $obj->visible(1) unless defined $obj->visible;
  108.             } elsif ($class eq 'MT::TBPing') {
  109.                 $obj->visible(1) unless defined $obj->visible;
  110.             } elsif ($class eq 'MT::Category') {
  111.                 my $key = lc($obj->label) . $obj->blog_id;
  112.                 if ($names{$class}{$key}++) {
  113.                     print "        Found duplicate category label '" .
  114.                           $obj->label;
  115.                     $obj->label($obj->label . ' ' . $names{$class}{$key});
  116.                     print "'; renaming to '" . $obj->label . "'\n";
  117.                 }
  118.                 # save the parent value for assignment at the end
  119.                 if ($obj->parent) {
  120.                     $cat_parent{$obj->id} = $obj->parent;
  121.                     $obj->parent(0);
  122.                 }
  123.             } elsif ($class eq 'MT::Trackback') {
  124.                 $obj->entry_id(0) unless defined $obj->entry_id;
  125.                 $obj->category_id(0) unless defined $obj->category_id;
  126.             } elsif ($class eq 'MT::Entry') {
  127.                 $obj->allow_pings(0)
  128.                     if defined $obj->allow_pings && $obj->allow_pings eq '';
  129.                 $obj->allow_comments(0)
  130.                     if defined $obj->allow_comments && $obj->allow_comments eq '';
  131.             }
  132.             $obj->save
  133.                 or die $obj->errstr;
  134.         }
  135.  
  136.         # fix up the category parents
  137.         foreach my $id (keys %cat_parent) {
  138.             my $cat = MT::Category->load($id);
  139.             $cat->parent( $cat_parent{$id} );
  140.             $cat->save;
  141.         }
  142.  
  143.         print "\n";
  144.     }
  145.  
  146.     if ($type eq 'postgres') {
  147.         print "Updating sequences\n";
  148.         my $dbh = MT::Object->driver->{dbh};
  149.         for my $class (keys %ids) {
  150.             print "    $class => $ids{$class}\n";
  151.             my $seq = 'mt_' . $class->datasource . '_' .
  152.                       $class->properties->{primary_key};
  153.             $dbh->do("select setval('$seq', $ids{$class})")
  154.                 or die $dbh->errstr;
  155.         }
  156.     }
  157.  
  158.     $cfg->SchemaVersion(MT->schema_version(), 1);
  159.     $cfg->save_config();
  160. };
  161. if ($@) {
  162.     print <<HTML;
  163.  
  164. An error occurred while loading data:
  165.  
  166. $@
  167.  
  168. HTML
  169. } else {
  170.     print <<HTML;
  171.  
  172. Done copying data from Berkeley DB to your SQL database! All went well.
  173.  
  174. HTML
  175. }
  176.  
  177. print "</pre>\n";
  178.